package com.csi.controller.role;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.Role;
import com.csi.pojo.Result;
import com.csi.service.RoleService;
import com.csi.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

@WebServlet("/role/findByIdController")
public class FindByIdController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Result<Map<String, Role>> result = new Result<>();

        Integer roleId = null;
        try {
            roleId = Integer.valueOf(req.getParameter("roleId"));
        } catch (Exception e) {
            result.setCode(400);
            result.setMsg("参数转换异常");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        RoleService roleService = new RoleServiceImpl();

        Map<String, Role> roleList = null;
        try {
            roleList = roleService.findById(roleId);
        } catch (Exception e) {
            result.setCode(400);
            result.setMsg("false");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        if (roleList != null) {
            result.setCode(200);
            result.setMsg("success");
            result.setDatas(roleList);
        } else {
            result.setCode(400);
            result.setMsg("false");
        }
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
